Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

262
Views
"$lookup with 'pipeline' may not specify 'localField' or 'foreignField'"

I'm new to mongoose & mongoDB, I have following query, which when run throws the follow error. It would be great if some can help me handle the issue and still get the same output

//interview.model.js => mongodb show name as interview
module.exports = mongoose.model('Interview', interviewSchema);
//candidate.model.js => mongodb show name as candidate
module.exports = mongoose.model('Candidate', candidateSchema);

const [result, err] = await of(Interview.aggregate([
         {
           $match: {
             ...filterQuery,
           }
         },
         {
           $lookup: {
             'from': 'candidates',
             'localField': 'candidateId',
             'foreignField': '_id',
             'as': 'candidateId',
             pipeline: [
               { $match: { 'candidateId.interviewCount': 0 }},
               { $project: { firstName:1, status:1, avatar:1 } }
             ]
           }
         },
       ]))
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

MongoDB 4.4 or below versions:

You can either use any one syntax of $lookup from 1) localField/ForeignField or 2) lookup with pipeline,

  • let to declare a variable candidateId you can use this id inside pipeline, that is called localField
  • pipeline push expression match using $expr and $eq because we are matching internal fields
  • $$ reference will allow to use variables in pipeline that is declared let
const [result, err] = await of(Interview.aggregate([
  { $match: { ...filterQuery } },
  { 
    $lookup: {
      'from': 'candidates',
      'as': 'candidateId',
      'let': { candidateId: "$candidateId" },
      'pipeline': [
        { 
          $match: { 
            $expr: { $eq: ["$$candidateId", "$_id"] },
            'candidateId.interviewCount': 0
          } 
        },
        { $project: { firstName:1, status:1, avatar:1 } }
      ]
    }
  }
]))

MongoDB 5.0 or above versions:

You query looks good as per latest version of mongodb, You can upgrade your mongodb version to mongodb latest version.

over 4 years ago · Santiago Trujillo Report

0

0

With pipeline inside $lookup, you cannot use localField or foreignField. See the MongoDB documentation for syntax and an example here...

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!